package com.ymttest.database.mapping.livesellerShop2cn;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.ymttest.database.mapping.livesellerShop2cn.*;
import com.ymttest.database.model.livesellerShop2cn.*;

public interface LiveRoomShop2cnMapper {
	@SelectProvider(type=LiveSellerTopSqlProvider.class, method="selectByExample")
//    @Results({
//        @Result(column="Id", property="id", jdbcType=JdbcType.INTEGER, id=true),
//        @Result(column="UserId", property="userId", jdbcType=JdbcType.INTEGER),
//        @Result(column="LiveId", property="liveId", jdbcType=JdbcType.INTEGER),
//        @Result(column="TopNum", property="topNum", jdbcType=JdbcType.INTEGER),
//        @Result(column="CreateTime", property="createTime", jdbcType=JdbcType.TIMESTAMP),
//        @Result(column="UpdateTime", property="updateTime", jdbcType=JdbcType.TIMESTAMP),
//        @Result(column="LiveCategoryId", property="liveCategoryId", jdbcType=JdbcType.INTEGER),
//        @Result(column="LiveCategoryName", property="liveCategoryName", jdbcType=JdbcType.VARCHAR)
//    })
	    List<LiveSellerTop> selectLiveSellerTopByExample(LiveSellerTopExample example);
//	@Select("select * from LiveActivitySubscribe where liveid=#{liveid} and userid=#{userid} and userid=#{userid} ")
//    List<Map> selectLiveActivitySubscribe();
	@SelectProvider(type=LiveActivitySubscribeSqlProvider.class, method="selectByExample")
//    @Results({
//        @Result(column="Id", property="id", jdbcType=JdbcType.INTEGER, id=true),
//        @Result(column="UserId", property="userId", jdbcType=JdbcType.INTEGER),
//        @Result(column="LiveId", property="liveId", jdbcType=JdbcType.INTEGER),
//        @Result(column="LiveActivityId", property="liveActivityId", jdbcType=JdbcType.INTEGER),
//        @Result(column="State", property="state", jdbcType=JdbcType.INTEGER),
//        @Result(column="CreateTime", property="createTime", jdbcType=JdbcType.TIMESTAMP),
//        @Result(column="UpdateTime", property="updateTime", jdbcType=JdbcType.TIMESTAMP)
//    })
    List<LiveActivitySubscribe> selectLiveActivitySubscribeByExample(LiveActivitySubscribeExample example);
	@Select("select * from liveroom where id=#{id}")
    List<LiveRoom> selectLiveRoomById(Map m);
	@Select("select * from liveroom order by id desc limit 1")
    List<LiveRoom> selectLastLiveRoom();
	@Select("select * from liveproduct where liveid=#{liveid}")
    List<Map> selectLiveproductByLiveid(Map m);
	@Select("<script>select * from livemember "+
	"<where> "+
		"<if test=\"uid!=null and uid!=''\"> "+
		"	userid= #{uid} "+
		"</if> "+
		"<if test=\"liveid!=null and liveid!=''\"> "+
		"	and liveid= #{liveid} "+
		"</if> "+
	"</where> "+
	"</script>")
    List<Map>  selectLivemember(Map m);
	@Select("select * from liveproduct where liveid=#{liveid} and istop=1")
    List<Map> selectTopLiveproductByLiveid(Map m);
	@Delete("delete from livemember where userid= #{uid} and liveid= #{liveid}")
    int deleteLivemember(Map m);
	@Select("SELECT * FROM liveproduct WHERE liveId = #{liveid} AND Status = 1 AND Action = 0	ORDER BY ProductNo Desc , SortNo asc limit ${skipNum}, ${pageSize}")
    List<Map> selectLiveproductToPage(Map m);
    /**
     * update liveroom set LiveStatus=#{livestatus} where liveid= #{liveid}
     * @param m
     * @return
     */
	@Update("update liveroom set LiveStatus=#{livestatus} where id= #{liveid}")
    int updateLiveRoomPushStatus(Map m);
    
    /**
     * update liveproduct 
		<set>
			<if test="istop!=null and istop!=''">
				istop= #{istop}
			</if>
			<if test="status!=null and status!=''">
				status= #{status}
			</if>
			<if test="action!=null and action!=''">
				action= #{action}
			</if>
			</set> 
			<where>LiveProductId= #{LiveProductId}</where> 
     * @param m
     * @return
     */
	@Update("<script> update liveproduct" + 
			"		<set>" + 
			"			<if test=\"istop!=null and istop!=''\">" + 
			"				istop= #{istop}" + 
			"			</if>" + 
			"			<if test=\"status!=null and status!=''\">" + 
			"				status= #{status}" + 
			"			</if>" + 
			"			<if test=\"action!=null and action!=''\">" + 
			"				action= #{action}" + 
			"			</if>" + 
			"			</set>" + 
			"			<where>LiveProductId= #{LiveProductId}</where> </script>")
    int updateLiveProductStatusById(Map m);
//    int selectCountLiveroom();
    //现在查询包含删除
    @Select("<script>select count(*) as count from liveroom "+
	"<where>"+
		 "<if test='uid !=null '>" + "userid=#{uid}	" + "</if>"+ 
		"<if test='livetype !=null '>" + "and livetype=#{livetype}	" + "</if>"+
		"and action!=-1 "+
		"</where>"+"</script>")
    Long selectCountLiveroom(Map m);
    @Select("<script>select * from liveproduct" + 
    		"		<where>" + 
    		"			<if test=\"liveid!=null and liveid!=''\">" + 
    		"				liveid= #{liveid}" + 
    		"			</if>" + 
    		"			<if test=\"productid!=null and productid!=''\">" + 
    		"				and productid= #{productid}" + 
    		"			</if>" + 
    		"		</where>" + 
    		"		ORDER BY ProductNo Desc , SortNo asc ,UpdateTime desc</script>")
    List<Map> selectLiveproductByIdAndPid(Map m);
    @Select("select * from liveroom where userid=#{userid} and livestatus < 7 and action >=0;")
    List<LiveRoom> selectRunningLiveRoomByUserid(Map m);
    @Select("select * from LiveRoomStat where liveid=#{id}")
    List<Map> selectLiveRoomStatById(Map m);
    @Update("<script>update liveroom " + 
    		"		<set>" + 
    		"			<if test=\"starttime!=null\">" + 
    		"				starttime= #{starttime}," + 
    		"			</if>" + 
    		"			<if test=\"endtime!=null\">" + 
    		"				endtime= #{endtime}," + 
    		"			</if>" + 
    		"			<if test=\"updatetime!=null\">" + 
    		"				updatetime= #{updatetime}," + 
    		"			</if>" + 
    		"		</set>" + 
    		"			where id=#{id}</script>")
    int updateLiveRoomTime(Map m);
    @Update("<script>update liveroom" + 
    		"		<set>" + 
    		"			<if test=\"updateTime!=null\">" + 
    		"				UpdateTime= #{updateTime}," + 
    		"			</if>" + 
    		"			<if test=\"endTime!=null\">" + 
    		"				EndTime= #{endTime}," + 
    		"			</if>" + 
    		"		</set>" + 
    		"		where id=#{id}</script>")
    int updateLiveRoomEndTimeAndUpdateTime(Map m);
    @Select("select * from liveonlinestat where liveid=#{liveid} order by createtime desc")
    List<Map> selectLastLiveOnlineStatById(@Param("liveid")Integer liveid);
    
    @Select("select * from livesubscribe WHERE LiveId=#{liveid} AND UserId=#{userid}")
    List<Map> selectLiveSubscribeById(@Param("liveid")Integer liveid,@Param("userid")Integer userid);
    
    @Select("select * from livevisitor WHERE deviceid=#{deviceid}" )
    List<Map> selectLivevisitorById(@Param("deviceid")String deviceid);
    @Select("select count(*) as count from liveroom where livestatus=#{status} and livetype=#{livetype} and action<>-1" )
    Long selectCountLiveroomByStatus(@Param("status")Integer status,@Param("livetype")Integer livetype);
    
    /**
     * update liveroom set action=#{action} where liveid= #{liveid}
     * @param m
     * @return
     */
    
    @Update("update liveroom set groupid=null where id= #{id}" )
    int updateLiveRoomGroupIdToNull(@Param("id")Integer id);
    
    @Update("update liveroom set action=#{action} where id= #{liveid}" )
    int updateLiveRoomAction(@Param("action")Integer action,@Param("liveid")Integer liveid);
    
    @Select("select * from messagetask WHERE msgtime=#{msgtime}" )
    List<Map> selectMessagetaskByMsgtime(@Param("msgtime")String msgtime);
    
    @Select("select count(*) from livedebug WHERE liveid=#{id}" )
    Integer selectLivedebugCountById(@Param("id")String id);
    
   
    @Select(" select * from livefootprint where userid=#{uid} order by updatetime desc" )
    List<Map> selectLivefootprintByUid(@Param("uid")Integer uid);
    @Select(" select * from livefootprint  order by updatetime desc limit 100" )
    List<Map> selectLivefootprint100();
    
    @Update("update livefootprint set updatetime=#{utime} where userid= #{uid}" )
    int updateLivefootprintUpdateTimeByUid(@Param("uid")Integer uid,@Param("ctime")Date ctime);
    
    /**
     * @param liveid
     * @param userid
     * @return
     */
    @Select("select * from liveroom where LiveType=1 order by id  limit 10 ")
    List<LiveRoom> selectWxLives();
    
    @Select("select * from useronlinestatus WHERE userid=#{uid}" )
    List<UserOnlineStatus> selectUserOnlinestatus(@Param("uid")String uid);
    
    @Select(" select * from livereplay where liveid=#{liveid}" )
    List<Map> selectLivereplayById(@Param("liveid")String liveid);
    
    @Select(" select * from liveroom where userid=#{uid} and livestatus<3 or liveroom.HistoryPlayUrl is not null" )
    List<Map> selectDisPlayLiveroomByUId(@Param("uid")String uid);
    
    @Select("select p.ProductId, p.LiveId from liveproduct p inner join liveroom r on p.LiveId = r.Id and r.LiveStatus < 7 and r.LiveStatus > 0 and r.`Action` = 0 and p.`Action` = 0 and p.`Status` = 1 inner join liveactivity a on r.Id = a.LiveId and a.ActivityState = 0 and a.StartTime > now() inner join liveactivityproduct ap on a.LiveActivityId = ap.LiveActivityId and p.ProductId = ap.ProductId and ap.`Status` = 1 and ap.StartTime > now() order by ap.StartTime asc ")
    List<Map> querySecKillProductList();
    
    @Select("select p.ProductId, p.LiveId from liveproduct p inner join liveroom r on p.LiveId = r.Id and r.LiveStatus < 7 and r.LiveStatus > 0 and r.`Action` = 0 and p.`Action` = 0 and p.`Status` = 1 and p.SoldStock > 0 and not exists( SELECT ProductId FROM LiveActivityProduct WHERE Status = 1 AND ProductId = p.ProductId ) order by p.SoldStock desc, p.ProductNo Desc, p.SortNo ASC")
    List<Map> queryHotSaleProducts();
    
    @Select("select * from liveactivity where liveactivityid=#{lid}")
    List<LiveActivity> selectLiveactivityById(@Param("lid")Integer lid);
    
    @Update("update Liveactivity set StartTime=#{stime} where liveactivityid= #{liveactivityid}" )
    int updateLiveactivityStartTimeByLiveactivityid(@Param("liveactivityid")Integer liveactivityid,@Param("stime")Date stime);
    
    @Select(" select * from liveactivity where liveid=#{lid} order by liveactivity.UpdateTime desc")
    List<LiveActivity> selectLiveactivityByLiveId(@Param("lid")Integer lid);
    
    @Select(" select * from liveactivityproduct where liveactivityid=#{lid} and  productid=#{pid} order by liveactivityproduct.UpdateTime desc")
    List<LiveActivityProduct> selectLiveactivityproduct(@Param("lid")Integer liveactivityid,@Param("pid")String pid);
    @Select(" select * from liveactivityproduct where productid=#{pid} order by UpdateTime desc")
    List<LiveActivityProduct> selectLiveactivityByProduct(@Param("pid")String pid);
    @Select(" select * from livecategory where action=0 order by orderNo")
    List<Map> selectLivecategory();
    @Select(" select * from SystemNotify where liveid=#{liveid} order by CreateTime desc")
    List<Map> selectSystemNotify(@Param("liveid")Integer liveid);
    
    @Update("update liveactivityproduct set ActivityStock=#{activityStock} ,SoldStock=#{soldStock}  where productid=#{pid} ")
    int updateLiveactivityByProduct(Map m);
    @Update("update liveactivityproduct set status=#{status} , EndTime=#{end} where productid=#{pid} ")
    int updateLiveactivityStatusByProduct(Map m);
    @Update("update SystemNotify set CreateTime=#{ctime} where liveid= #{liveid}" )
    int updateSystemNotifyCreateTime(@Param("liveid")Integer liveid,@Param("ctime")Date ctime);
    
    @Select("select * from GlobalUserOnlineStatus WHERE userid=#{uid}" )
    List<UserOnlineStatus> selectGlobalUserOnlineStatus(@Param("uid")String uid);
    
    @Select("select * from livechatmessage where MsgSeq=#{MsgSeq} order by createtime desc")
    List<LiveChatMessagePo> selectLivehatMessageByMsgSeq(@Param("MsgSeq")Long MsgSeq);
    @Select(" select * from livenotice where Id=#{id}")
    List<Map> selectLivenotice(@Param("id")Integer id);
//    @Select(" select * from speakingnotallowed where userid=#{userid} order by id desc")
    @Select("<script>select  *   from speakingnotallowed "+
    	    "<where> "+
    	       "<if test='userid != null' >"+
    	      "  and userid=#{userid}"+
    	     " </if>"+
    	     "<if test='status != null' >"+
    	     "   and state = #{state}"+
    	      "</if></where>  order by updatetime desc, id asc</script>")
    List<SpeakingNotAllowed> selectSpeakingNotAllowed(Map m);
    
    @Select("SELECT * FROM liveinheritrelation WHERE roomid=#{roomid} AND inherituserid=#{iuserid}")
    List<Map> selectLiveInheritrelation(Map m);
}